# -*- coding: utf-8 -*-
from __future__ import unicode_literals

# Create your views here.
from django.db.models import Q
from django.http import JsonResponse

from app_device.models import *


def getDevice(request):
    deviceid = request.GET.get("id", "")
    data = dict(status=0, message="", data=[])
    try:
        if id:
            # 判断设备是否存在
            devices = Devices.objects.filter(Q(id=deviceid), Q(status=True))
            if devices:
                device = devices[0]
                # 设备图片
                imgs = device.deviceimg_set.all()
                images = [w.url for w in imgs]
                device_data = dict(name=device.name, code=device.code, qr_code=device.qr_code, brand=device.brand,
                                   model=device.model,
                                   number=device.number, supplier=device.supplier, supplier_phone=device.supplier_phone,
                                   ip=device.ip, address=device.address,
                                   pile_no=device.pile_no, construction_units=device.construction_units,
                                   construction_units_contact=device.construction_units_contact,
                                   construction_units_phone=device.construction_units_phone,
                                   use_units_contact=device.use_units_contact,
                                   maintenance_units=device.maintenance_units,
                                   maintenance_units_contact=device.maintenance_units_contact,
                                   maintenance_units_phone=device.maintenance_units_phone, images=images)
                data = dict(status=1, message="查询成功", data=device_data)
            else:
                raise Exception("设备不存在")
        else:
            raise Exception("传入参数错误,请检查")
    except Exception as e:
        data['message'] = "查询失败，%s" % e.message
    return JsonResponse(data)
